﻿using System;
using System.Data;
using System.Drawing;
using WMSABL;
using WMSABL.Warehouse;
using WMSAML.Warehouse;
using System.Windows.Forms;
using System.Collections.Generic;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.Warehouse
{
    public partial class Postion_LayoutFrm : Form
    {
        #region
        private int Postion_LayoutNID = 0;
        private string Layout_Field = "";
        List<Point> lists = new List<Point>();
        PublicDataSet bll_Datset = new PublicDataSet();
        PD_PostionToLayoutBLL bll_PostionToLayout = new PD_PostionToLayoutBLL();
        PD_PostionToLayoutEntity entity_PostionToLayout = new PD_PostionToLayoutEntity();
        WMSAML.Warehouse.Model.T_Position tpmode = new WMSAML.Warehouse.Model.T_Position();
        WMSAML.Warehouse.BLL.T_Position tpbll = new WMSAML.Warehouse.BLL.T_Position();
        public Postion_LayoutFrm(int layoutID, string fieldValues, List<Point> list, string V1, string V2)
        {
            InitializeComponent();
            this.Postion_LayoutNID = layoutID;
            this.Layout_Field = fieldValues;
            this.lists = list;
            if (V1 != "") var1 = V1;
            if (V2 != "") var2 = V2;
        }
        private void Postion_LayoutFrm_Load(object sender, EventArgs e)
        {
            this.DBind();
            this.comboBoxArea.Text = var1;
            this.txtPostionCode.Text = var2;
            this.btnSearch_Click(sender, e);
        }
        #endregion

        #region 绑定
        public void DBind()
        {
            this.comboBoxStauts.SelectedIndex = 0;
            DataSet ds1 = new DataSet();
            ds1 = bll_Datset.GetDataSet("PD_Warehouse", "");
            this.comboBoxWarehouse.DataSource = ds1.Tables[0].DefaultView;
            this.comboBoxWarehouse.ValueMember = "NID";
            this.comboBoxWarehouse.DisplayMember = "WarehouseNO";
            this.comboBoxWarehouse.SelectedIndex = 0;

            DataSet ds2 = new DataSet();
            ds2 = bll_Datset.GetDataSet("PD_Area", "");
            this.comboBoxArea.DataSource = ds2.Tables[0].DefaultView;
            this.comboBoxArea.ValueMember = "NID";
            this.comboBoxArea.DisplayMember = "AreaNO";
            this.comboBoxArea.SelectedIndex = 0;
        }
        private void DBind_Postion(string strPostion)
        {
            DataSet ds = new DataSet();
            ds = bll_Datset.GetDataSet("  select  Warehouse_id as 'NID' , Subwarehouse_no as 'WarehouseNO',Region_no as 'AreaNO', Warehouse_no as 'PositionNO',BK01 as 'Designated_Stauts' from T_Position " + strPostion);
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            this.dataGridView1.ClearSelection();
        }
        private void DBind_Postion_Layout(string str2)
        {
            DataSet ds = new DataSet();
            ds = bll_Datset.GetDataSet("SELECT NID,Postion_NID,PositionNO,Layout_NID FROM PD_PostionToLayout " + str2);
            this.dataGridView2.AutoGenerateColumns = false;
            this.dataGridView2.DataSource = ds.Tables[0].DefaultView;
            this.dataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridView2.ClearSelection();
        }

        #endregion
      
        #region 查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strWhere = " WHERE  BK02=1 ";
            string BoxStauts = comboBoxStauts.Text.Trim();
            string BoxWarehouse = comboBoxWarehouse.Text.Trim();
            string BoxArea = comboBoxArea.Text.Trim();
            string PostionCode = txtPostionCode.Text.Trim();
            if (BoxStauts != "")
                strWhere += " AND  BK01 LIKE '%" + BoxStauts + "' ";
            if (BoxWarehouse != "")
                strWhere += " AND Subwarehouse_no LIKE '%" + BoxWarehouse + "'";
            if (BoxArea != "")
                strWhere += "  AND   Region_no LIKE '%" + BoxArea + "%'";
            if (PostionCode != "")
                strWhere += "  AND  Warehouse_no LIKE '%" + PostionCode + "%'";

            this.DBind_Postion(strWhere);
            this.DBind_Postion_Layout(" WHERE Layout_NID='" + Postion_LayoutNID + "' AND Layout_Field='" + Layout_Field + "' AND  Stauts=0 ");
        }
        public string var1 = "";
        public string var2 = "";
        private void btnClose_Click(object sender, EventArgs e)
        {
            var1 = this.comboBoxArea.Text.Trim();
            var2 = this.txtPostionCode.Text.Trim();
            this.Close();
        }
        #endregion

        #region 事件
        #region 添加信息
        private int Postion_NID = 0;
        private string Postion_Code = "";
        public bool isture = false;
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Postion_NID = 0;
                Postion_Code = "";
                if (this.dataGridView1.Rows.Count > 0)
                {
                    this.dataGridView1.Rows[e.RowIndex].Selected = true;
                    if (dataGridView1.CurrentRow.Cells["Designated_Stauts"].Value.ToString() == "未分配")
                    {
                        #region
                        Postion_NID = Convert.ToInt32(dataGridView1.CurrentRow.Cells["NID"].Value.ToString());
                        Postion_Code = dataGridView1.CurrentRow.Cells["PositionNO"].Value.ToString();
                        tpmode = new WMSAML.Warehouse.Model.T_Position();
                        tpmode = tpbll.GetModel(Postion_NID);
                        tpmode.BK01 = "已分配";

                        if (tpbll.Update(tpmode))// 更改仓位分配状态
                        {
                            entity_PostionToLayout = new PD_PostionToLayoutEntity();
                            entity_PostionToLayout.Postion_NID = Postion_NID;
                            entity_PostionToLayout.PositionNO = Postion_Code;
                            entity_PostionToLayout.Layout_NID = Postion_LayoutNID;
                            entity_PostionToLayout.Layout_Field = Layout_Field;

                            entity_PostionToLayout.PointTop = lists[0].X + "," + lists[0].Y;//1

                            entity_PostionToLayout.PointL = lists[1].X + "," + lists[1].Y;//2
                            entity_PostionToLayout.PointR = lists[2].X + "," + lists[2].Y;//3

                            entity_PostionToLayout.PointU = lists[3].X + "," + lists[3].Y;//4
                            entity_PostionToLayout.PointD = lists[4].X + "," + lists[4].Y;//5
                            if (bll_PostionToLayout.GetEntityByPotionNID(Postion_NID) != null)
                            {
                                entity_PostionToLayout.Mark = "此仓位重新分配";
                            }
                            else
                            {
                                entity_PostionToLayout.Stauts = 0;
                            }
                            entity_PostionToLayout.CreateTime = DateTime.Now;
                            int result = bll_PostionToLayout.Add(entity_PostionToLayout);
                            bll_Datset.ExecuteSQL("update dbo.PD_PostionToLayout set Stauts=1 where NID!='" + result + "' and PositionNO='" + Postion_Code + "'");
                            this.dataGridView1.Rows.RemoveAt(e.RowIndex);
                            this.btnSearch_Click(sender, e);
                            isture = true;
                        }
                        #endregion
                    }
                    else
                    {
                        MessageClass.warning("该仓位已分配！");
                    }
                }
            }
            catch (Exception)
            {
                MessageClass.error("选择错误！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageClass.enquire("确认加入选中的仓位？") == DialogResult.OK)
            {
                string[] selectedContents = new string[dataGridView1.SelectedRows.Count];
                for (int i = 0; i < selectedContents.Length; i++)
                {
                    Postion_NID = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value.ToString());
                    tpmode = new WMSAML.Warehouse.Model.T_Position();
                    tpmode = tpbll.GetModel(Postion_NID);
                    tpmode.BK01 = "已分配";
                    tpbll.Update(tpmode);

                    entity_PostionToLayout = new PD_PostionToLayoutEntity();
                    entity_PostionToLayout.Postion_NID = Postion_NID;
                    entity_PostionToLayout.PositionNO = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    entity_PostionToLayout.Layout_NID = Postion_LayoutNID;
                    entity_PostionToLayout.Layout_Field = Layout_Field;

                    entity_PostionToLayout.PointTop = lists[0].X + "," + lists[0].Y;//1

                    entity_PostionToLayout.PointL = lists[1].X + "," + lists[1].Y;//2
                    entity_PostionToLayout.PointR = lists[2].X + "," + lists[2].Y;//3

                    entity_PostionToLayout.PointU = lists[3].X + "," + lists[3].Y;//4
                    entity_PostionToLayout.PointD = lists[4].X + "," + lists[4].Y;//5
                    entity_PostionToLayout.Stauts = 0;
                    entity_PostionToLayout.CreateTime = DateTime.Now;
                    bll_PostionToLayout.Add(entity_PostionToLayout);
                    isture = true;
                }
                this.btnSearch_Click(sender, e);
            }
        }
        #endregion

        #region 移除
        PD_StockBLL bll_PD_Stock = new PD_StockBLL();
        /// <summary>
        /// 移除仓位地址
        /// </summary>
        private int positionNID_Rigth = 0;
        /// <summary>
        /// 仓位分配标识
        /// </summary>
        private int Layout_NIDs = 0;
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (this.dataGridView2.Rows.Count > 0 && e.ColumnIndex > -1)
                {
                    this.dataGridView2.Rows[e.RowIndex].Selected = true;
                    positionNID_Rigth = Convert.ToInt32(this.dataGridView2.Rows[e.RowIndex].Cells["Postion_NIDs"].Value.ToString());

                    Layout_NIDs = Convert.ToInt32(this.dataGridView2.Rows[e.RowIndex].Cells["NIDs"].Value.ToString());
                    string Postion_Codes = this.dataGridView2.Rows[e.RowIndex].Cells["PositionNOs"].Value.ToString();
                    if (!bll_PD_Stock.PostionQuantity(Postion_Codes))
                    {
                        if (MessageClass.enquire("确认移除此仓位？") == DialogResult.OK)
                        {
                            tpmode = new WMSAML.Warehouse.Model.T_Position();
                            tpmode = tpbll.GetModel(positionNID_Rigth);
                            tpmode.BK01 = "未分配";
                            ;
                            if (tpbll.Update(tpmode)) //更改仓位分配状态
                            {
                                entity_PostionToLayout = new PD_PostionToLayoutEntity();
                                entity_PostionToLayout = bll_PostionToLayout.GetEntity(Layout_NIDs);
                                entity_PostionToLayout.Stauts = 1;
                                entity_PostionToLayout.Mark = "此仓位重新分配";
                                bll_PostionToLayout.Update(entity_PostionToLayout);

                                this.btnSearch_Click(sender, e);
                            }
                        }
                    }
                    else
                    {
                        if (MessageClass.enquire("该仓位地址存有部品信息,确认移除此仓位？\n操作方式1：移除此地址部品信息\n操作方式2：称位，即把此地址仓位移动到其他区域。") == DialogResult.OK)
                        {
                            tpmode = new WMSAML.Warehouse.Model.T_Position();
                            tpmode = tpbll.GetModel(positionNID_Rigth);
                            tpmode.BK01 = "未分配";
                            if (tpbll.Update(tpmode))//更改仓位分配状态
                            {
                                entity_PostionToLayout = new PD_PostionToLayoutEntity();
                                entity_PostionToLayout = bll_PostionToLayout.GetEntity(Layout_NIDs);
                                entity_PostionToLayout.Stauts = 1;
                                entity_PostionToLayout.Mark = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + " 此仓位重新分配";
                                bll_PostionToLayout.Update(entity_PostionToLayout);

                                this.btnSearch_Click(sender, e);
                            }
                            MessageClass.right("请移位相关部品信息！");
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageClass.error("操作失败！");
            }
        }
        #endregion

        #region
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView2.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView2.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        Point downPoint;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            downPoint = new Point(e.X, e.Y);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - downPoint.X,
                    this.Location.Y + e.Y - downPoint.Y);
            }
        }
        #endregion

    }
}
